package com.bkfm.framework.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yak.common.utils.RandomUtil;
import org.yak.core.service.BaseService;
import org.yak.core.web.ResultSet;

import com.bkfm.framework.core.api.service.IBkfmTableMetaServer;
import com.bkfm.framework.core.api.store.IBkfmTableMetaStore;
import com.bkfm.framework.core.entity.BkfmDataRecord;
import com.bkfm.framework.core.entity.BkfmTableMetaData;
import com.bkfm.framework.core.utils.QueryCall;
import com.bkfm.framework.sys.api.service.IBkfmMenuService;
import com.bkfm.framework.sys.api.store.IBkfmMenuStore;
import com.bkfm.framework.sys.entity.BkfmMenu;
import com.bkfm.framework.sys.entity.BkfmMenuBtnPermis;
import com.bkfm.framework.sys.entity.BkfmModelBtnPermis;
import com.bkfm.framework.sys.entity.BkfmUsers;
import com.bkfm.framework.sys.utils.BkfmUserUtils;

import net.sf.json.JSONObject;

@Service
@Transactional
public class BkfmMenuService extends BaseService<BkfmMenu> implements IBkfmMenuService{

	@Resource
	private IBkfmMenuStore bkfmMenuStore;
	
	@Resource
	private IBkfmTableMetaServer tableMetaService;
	
	@Resource
	private IBkfmTableMetaStore tableMetaStore;
	/**
	 * 查询用户对应菜单
	 * @param bkfmUser
	 * @return
	 * @throws Exception
	 */
	public List<BkfmMenu> queryUserMenuList(BkfmUsers bkfmUser)throws Exception{
		if(BkfmUserUtils.getCurrentUser().getMenuList()==null || BkfmUserUtils.getCurrentUser().getMenuList().isEmpty()) {
			List<BkfmMenu> menulist = null;
			if(BkfmUserUtils.getCurrentUser().isSuperAdminUser()) {
				//管理员所有菜单
				menulist = bkfmMenuStore.selectBkfmMenuList();
			}else {
				//查询用户对应菜单
				menulist = bkfmMenuStore.selectUserBkfmMenuList(bkfmUser);
			}
			BkfmUserUtils.getCurrentUser().setMenuList(menulist);
			return menulist;
		}
		return BkfmUserUtils.getCurrentUser().getMenuList();
	}
	
	/**
	 * 添加用户桌面快捷菜单
	 * @param menu
	 * @throws Exception
	 */
	public void addUserDesktopMenu(BkfmMenu menu)throws Exception{
		BkfmUsers user = BkfmUserUtils.getCurrentUser().getUser();
		//查询当前用户桌面快捷菜单最大排序号
		tableMetaService.queryResultBySql("SELECT MAX(ORDER_BY) AS ORDER_BY,COUNT(0) AS COUNT_NUM FROM BLK_USER_SHORTCUTS WHERE USER_NO='"+user.getUser_no()+"'", new QueryCall() {
			@Override
			public void callBackList(List<ResultSet> list) throws Exception {
				int orderBy = 1;
				if(list !=null && !list.isEmpty()) {
					Map<String,Object> mapValue = list.get(0);
					int count = Integer.parseInt(String.valueOf(mapValue.get("COUNT_NUM")));
					if(count>0) {
						orderBy = Integer.parseInt(String.valueOf(mapValue.get("ORDER_BY")))+1;
					}
				}
				//保存
				BkfmDataRecord record = new BkfmDataRecord();
				record.setRwid(RandomUtil.getRandom());
				record.setObj_path("ROOT/XTGLPT/XTSZ/BLK_USER_SHORTCUTS");
				record.setRecord_state(BkfmDataRecord.RECORD_STATE_NEW);
				JSONObject recordData = new JSONObject();
				recordData.put("USER_NO", user.getUser_no());
				recordData.put("MENU_RWID", menu.getRwid());
				recordData.put("ORDER_BY", orderBy);
				record.setRecord_data(recordData.toString());
				tableMetaService.addModelMetaData(record);
			}
		});
	}
	
	/**
	 * 查询用户桌面快捷菜单
	 * @param bkfmUser
	 * @return
	 * @throws Exception
	 */
	public List<BkfmMenu> queryUserDesktopMenuList(BkfmUsers bkfmUser)throws Exception{
		return bkfmMenuStore.selectUserDesktopMenuList(bkfmUser);
	}
	
	/**
	 * 查询用户所属菜单页面的功能按钮
	 * @param bkfmUser
	 * @return
	 * @throws Exception
	 */
	public Map<String,List<String>> queryUserMenuBtnPermis(BkfmUsers bkfmUser)throws Exception{
		Map<String,List<String>> menuPageBtnMap = new HashMap<String,List<String>>();
		if(BkfmUserUtils.getCurrentUser().isSuperAdminUser()) {
			//系统管理员
			//查询系统共用按钮
			List<BkfmMenuBtnPermis> sysBtnList = bkfmMenuStore.selectSysBtn();
			
			//查询系统菜单自定义按钮
			List<BkfmMenuBtnPermis> userMenuBtnList = bkfmMenuStore.selectUserMenuBtn();
			List<BkfmMenu> menuList = BkfmUserUtils.getCurrentUser().getMenuList();
			for(BkfmMenu bkfmMenu : menuList) {
				if(bkfmMenu.getMenu_type().equals("1")) {
					String menu_id = bkfmMenu.getRwid();
					//添加系统固定按钮
					List<String> btnList = new ArrayList<String>();
					for(BkfmMenuBtnPermis sysBtn : sysBtnList) {
						btnList.add(sysBtn.getBtn_code());
					}
					//添加用户自定义按钮
					for(BkfmMenuBtnPermis userBtn : userMenuBtnList) {
						if(userBtn.getMenu_rwid().equals(menu_id)) {
							btnList.add(userBtn.getBtn_code());
						}
					}
					menuPageBtnMap.put(menu_id, btnList);
				}
			}
		}else {
			//非管理员
			List<BkfmMenuBtnPermis> userMenuBtnList = bkfmMenuStore.selectUserMenuBtnPermis(bkfmUser);
			for(BkfmMenuBtnPermis btnPermis : userMenuBtnList) {
				String menu_rwid = btnPermis.getMenu_rwid();
				String btn_code = btnPermis.getBtn_code();
				if(menuPageBtnMap.containsKey(menu_rwid)) {
					List<String> btnList = menuPageBtnMap.get(menu_rwid);
					if(!btnList.contains(btn_code)) {
						btnList.add(btn_code);
					}
				}else {
					List<String> btnList = new ArrayList<String>();
					btnList.add(btn_code);
					menuPageBtnMap.put(menu_rwid, btnList);
				}
			}
		}
		return menuPageBtnMap;
	}
	
	/**
	 * 查询用户对应的业务模型功能权限
	 * @param bkfmUser
	 * @return
	 * @throws Exception
	 */
	public Map<String,List<String>> queryUserModelBtnPermis(BkfmUsers bkfmUser)throws Exception{
		Map<String,List<String>> menuModelBtnMap = new HashMap<String,List<String>>();
		if(BkfmUserUtils.getCurrentUser().isSuperAdminUser()) {
			//查询业务模型定义的全部按钮
			List<BkfmMenuBtnPermis> sysBtnList = bkfmMenuStore.selectSysBtn();
			//系统管理员,查询所有业务模型功能按钮
			List<BkfmTableMetaData> tableModelMetaList = tableMetaStore.selectAllTableViewModel();//
			for(BkfmTableMetaData tableModelMeta : tableModelMetaList) {
				List<String> btnPermisList = new ArrayList<String>();
				String objPath = tableModelMeta.getObj_path();
				for(BkfmMenuBtnPermis btnPermis : sysBtnList) {
					btnPermisList.add(btnPermis.getBtn_code());
				}
				menuModelBtnMap.put(objPath, btnPermisList);
			}
		}else {
			//非系统管理员
			List<BkfmModelBtnPermis> userModelBtnList = bkfmMenuStore.selectUserModelBtnPermis(bkfmUser);
			for(BkfmModelBtnPermis userModelBtn : userModelBtnList) {
				Object objPath = userModelBtn.getObj_path();
				if(menuModelBtnMap.containsKey(objPath)) {
					List<String> btnPermisList = (List<String>)menuModelBtnMap.get(objPath);
					btnPermisList.add(userModelBtn.getBtn_code());
					menuModelBtnMap.put(objPath.toString(), btnPermisList);
				}else {
					List<String> btnPermisList = new ArrayList<String>();
					btnPermisList.add(userModelBtn.getBtn_code());
					menuModelBtnMap.put(objPath.toString(), btnPermisList);
				}
			}
		}
		
		return menuModelBtnMap;
	}
}
